package mybatis;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.fastjson.JSON;

import mybatis.dao.IPermitApplyDAO;
import mybatis.dao.StudentDao;
import mybatis.entity.PermitApplyDO;
import mybatis.entity.Student;

public class Test {
    private static void test() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("dispatcher-servlet.xml");
        SqlSessionFactory sessionFactory = (SqlSessionFactory) ac.getBean("sqlSessionFactory");
        SqlSession sqlSession = sessionFactory.openSession();
        IPermitApplyDAO permitApplyDAO = sqlSession.getMapper(IPermitApplyDAO.class);
        PermitApplyDO permitApplyDO = permitApplyDAO.selectByAnnotation1(169l);
        System.out.println(JSON.toJSONString(permitApplyDO));
    }

    public static void main(String[] args) {
        // test();
        multiDatasource();
        // try {
        // AnnotationEntity.class.getDeclaredField("objectFlag");
        // } catch (NoSuchFieldException | SecurityException e) {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
        // }
    }

    private static void multiDatasource() {
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, "development");
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // PermitApplyDO permitApplyDO1 = (PermitApplyDO)
        // sqlSession.selectOne("mybatis.dao.IPermitApplyDAO.selectByPrimaryKey",
        // 169l);
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
         Student student = studentDao.queryInfo2(2l);
         System.out.println(JSON.toJSONString(student));
//        List<Student> students = studentDao.queryInfoList2();
//        System.out.println(JSON.toJSONString(students));
    }
}
